﻿namespace AnyOffice.WebControls
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), ToolboxData("<{0}:HFCheckBoxList runat=server></{0}:HFCheckBoxList>")]
    public class HFCheckBoxList : CheckBoxList
    {
        private string strChineseName;
        private string strPropertyName;
        private string strPropertyValue;

        public bool BindCbl(string[] strValue, string[] strName)
        {
            try
            {
                this.Items.Clear();
                for (int i = 0; i < strValue.Length; i++)
                {
                    this.Items.Add(new ListItem(strName[i].ToString().Trim(), strValue[i].ToString().Trim()));
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool BindCbl(ArrayList arrListValue, ArrayList arrListName)
        {
            try
            {
                this.Items.Clear();
                for (int i = 0; i < arrListValue.Count; i++)
                {
                    this.Items.Add(new ListItem(arrListName[i].ToString().Trim(), arrListValue[i].ToString().Trim()));
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool BindCbl(DataSet ds, string TextField, string ValueField)
        {
            DataView defaultView = ds.Tables[0].DefaultView;
            return this.BindCbl(defaultView, TextField, ValueField);
        }

        public bool BindCbl(DataView dv, string TextField, string ValueField)
        {
            try
            {
                this.Items.Clear();
                this.DataSource = dv;
                this.DataValueField = ValueField;
                this.DataTextField = TextField;
                this.DataBind();
                return true;
            }
            catch
            {
                return false;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.CssClass = "dibg";
        }

        public void SetValue(string strValue)
        {
            try
            {
                this.SelectedValue = strValue;
            }
            catch
            {
            }
        }

        [Description("属性对应的中文名，目的是在数据校验的时候抛出异常提示时使用"), Browsable(true), Bindable(true), Category("HFServer")]
        public string ChineseName
        {
            get
            {
                if (base.Attributes["ChineseName"] != null)
                {
                    this.strChineseName = base.Attributes["ChineseName"].Trim();
                }
                return this.strChineseName;
            }
            set
            {
                this.strChineseName = value;
                base.Attributes.Add("ChineseName", this.strChineseName);
            }
        }

        [Browsable(true), Description("对应实体类属性的名称"), Category("HFServer"), Bindable(true)]
        public string PropertyName
        {
            get
            {
                if (base.Attributes["PropertyName"] != null)
                {
                    this.strPropertyName = base.Attributes["PropertyName"].Trim();
                }
                return this.strPropertyName;
            }
            set
            {
                this.strPropertyName = value;
                base.Attributes.Add("PropertyName", this.strPropertyName);
            }
        }

        [Description("保存控件对应数据库字段当前行的原值"), Browsable(true), Bindable(true), Category("HFServer")]
        public string PropertyValue
        {
            get
            {
                if (base.Attributes["PropertyValue"] != null)
                {
                    this.strPropertyValue = base.Attributes["PropertyValue"];
                }
                return this.strPropertyValue;
            }
            set
            {
                this.strPropertyValue = value;
                base.Attributes.Add("PropertyValue", this.strPropertyValue);
            }
        }
    }
}

